/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.xb.ctx.attr.factory;

import axescon.xacml.api.ExpressionEvalIndeterminateException;
import axescon.xacml.model.ctx.AttributeValue;
import axescon.xacml.model.shared.attr.HexBinaryAttribute;
import axescon.xacml.xb.ctx.XBAttributeValueFactory;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlHexBinary;
import x0ContextSchemaOs.oasisNamesTcXacml2.AttributeValueType;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: argyn
 * Date: Jun 18, 2005
 * Time: 2:19:37 PM
 * To change this template use File | Settings | File Templates.
 */
public class HexBinaryXBAttrValueFactory implements XBAttributeValueFactory {

/*
    public AttributeValue toAttributeValueType(HexBinaryAttribute v) {
        AttributeValue ret = AttributeValue.Factory.newInstance();
        XmlCursor xmlCursor = ret.newCursor();
        xmlCursor.toNextToken();
        xmlCursor.insertChars(v.encode());
        xmlCursor.dispose();
        return ret;
    }
    public AttributeValue toAttributeValueType(AttributeValue v) {
        return toAttributeValueType((HexBinaryAttribute) v);
    }
*/

    public HexBinaryAttribute getHexBinaryAttributeValue(AttributeValueType vt) {
        try {
            return new HexBinaryAttribute(XmlHexBinary.Factory.parse(vt.xmlText()).getByteArrayValue());
        } catch (XmlException e) {
            throw new IllegalArgumentException(e.getMessage());
        }

    }


    public AttributeValue getAttributeValue(AttributeValueType vt) {
        return getHexBinaryAttributeValue(vt);
    }

    public axescon.xacml.model.policy.AttributeValue getAttributeValue(x0PolicySchemaOs.oasisNamesTcXacml2.AttributeValueType vt) {
        return getHexBinaryAttribute(vt);
    }


    public HexBinaryAttribute getHexBinaryAttribute(x0PolicySchemaOs.oasisNamesTcXacml2.AttributeValueType vt) {
        try {
            return new HexBinaryAttribute(XmlHexBinary.Factory.parse(vt.xmlText()).getByteArrayValue());
        } catch (XmlException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }

    public axescon.xacml.model.policy.AttributeValue[] getAttributeValues(XmlCursor cur) throws ExpressionEvalIndeterminateException {
        if (!cur.hasNextSelection()) return null;
        ArrayList ret = new ArrayList(1);
        while (cur.hasNextSelection()) {
            cur.toNextSelection();
            try {
                ret.add(new HexBinaryAttribute(XmlHexBinary.Factory.parse(cur.xmlText()).getByteArrayValue()));
            } catch (XmlException e) {
                throw new ExpressionEvalIndeterminateException(e);
            }
        }
        return (axescon.xacml.model.policy.AttributeValue[]) ret.toArray(new axescon.xacml.model.policy.AttributeValue[ret.size()]);
    }
}
